    //
    //  XOClubMessageCell.m
    //  didigo
    //
    //  Created by JS_Coder on 2019/2/18.
    //  Copyright © 2019 lifayu. All rights reserved.
    //

#import "XOClubMessageCell.h"
#import "EJSClubCommentModel.h"
#import "XOClubCommentCell.h"
#import <SDWebImage/UIButton+WebCache.h>
#import "XOClubImageViewController.h"
#import "XOClubViewModel.h"
#import "XOLikeButton.h"
#import "EJSClubMomentLoveModel.h"

@interface XOClubMessageCell()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *messageLabel;
@property (weak, nonatomic) IBOutlet UIView *imageBgView;
@property (weak, nonatomic) IBOutlet XOLikeButton *likeButton;
@property (weak, nonatomic) IBOutlet UIButton *commentButton;
@property (weak, nonatomic) IBOutlet UIView *baseView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeight;
@property (weak, nonatomic) IBOutlet UITableView *commentTableView;
@property (weak, nonatomic) IBOutlet UIView *middleView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *loveViewHeightConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *commentTableHeightConstraints;
@property (weak, nonatomic) IBOutlet UIImageView *commentIcon;
@property (weak, nonatomic) IBOutlet UIView *likeAndCommentView;
@property (weak, nonatomic) IBOutlet UIView *likeIconsBaseView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *likeAndCommentViewConstranit;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *likeViewConstranit;

@end

@implementation XOClubMessageCell

#pragma 初始化
- (void)awakeFromNib {
    [super awakeFromNib];
    [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        // Initialization code
    self.baseView.layer.cornerRadius = 8;
    self.baseView.layer.shadowRadius = 3;
    self.baseView.layer.shadowOffset = CGSizeMake(0, 2);
    self.baseView.layer.shadowColor = AppMainColor.CGColor;
    self.baseView.layer.shadowOpacity = 0.5;
    self.commentTableView.dataSource = self;
    self.commentTableView.rowHeight = 30;
    
    [self.commentTableView registerNib:[UINib nibWithNibName:@"XOClubCommentCell" bundle:nil] forCellReuseIdentifier:@"XOClubCommentCell"];

}

#pragma 初始化数据
- (void)setMessageModel:(EJSClubMessageModel *)messageModel {
    _messageModel = messageModel;
    XOClubViewModel * viewModel = [XOClubViewModel shared];
    [self setData];
    [self setImages];
    if (!viewModel.isComment) {
        [self setLikeImages];
    }

    [self.commentTableView reloadData];
}

#pragma 设置文字数据以及是否显示
-(void) setData {
    XOClubViewModel * viewModel = [XOClubViewModel shared];
    NSURL *url = [NSURL URLWithString:[self.messageModel.user.avatar stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    if (url) {
        [self.iconImageView sd_setImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"01"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        }];
    }else {
        [self.iconImageView setImage:[UIImage imageNamed:@"01"] forState:UIControlStateNormal];
    }
    self.messageLabel.text = self.messageModel.content;
    self.nameLabel.text = self.messageModel.user.name;
    [self.likeButton setTitle:self.messageModel.loveTotal forState:UIControlStateNormal];
    [self.commentButton setTitle:self.messageModel.commentTotal forState:UIControlStateNormal];
    self.commentTableHeightConstraints.constant = self.messageModel.commentsHeight;
    self.loveViewHeightConstraint.constant = self.messageModel.likesHeight;

    [self.commentIcon setHidden: NO];
    [self.middleView setHidden:NO];
    [self.commentTableView setHidden:NO];
    [self.likeAndCommentView setHidden:NO];

    if ([self.messageModel.commentTotal isEqualToString: @"0"]) {
        [self.commentIcon setHidden: YES];
        [self.commentTableView setHidden:YES];
    }else {
        [self.commentTableView reloadData];
    }

    if ([self.messageModel.loveTotal isEqualToString:@"0"]) {
        [self.middleView setHidden: YES];
    }else {
        [self.commentTableView reloadData];
    }

    if (self.messageModel.hasLoved.intValue == 1) {
        [self.likeButton setSelected:YES];
    }else {
        [self.likeButton setSelected:NO];
    }
    
    if (viewModel.isComment) {
        self.likeAndCommentViewConstranit.constant = 0;
        self.likeViewConstranit.constant = 0;
        [self.likeAndCommentView setHidden:YES];
        [self.middleView setHidden: YES];
        [self.commentIcon setHidden: YES];
        [self.commentTableView setHidden:YES];
    }
}

    //MARK: - LikeIcons
- (void) setLikeImages {
    if (self.messageModel.loveTotal.intValue > 0) {
        CGFloat WH = self.likeIconsBaseView.frame.size.height - 10;
        int counts = self.likeIconsBaseView.frame.size.width / (WH + CommonMargin * 0.5);

        [self.messageModel.momentLove enumerateObjectsUsingBlock:^( EJSClubMomentLoveModel* obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx >= counts) {
                return;
            }
            UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(idx * WH + (idx * (CommonMargin * 0.5)) , (self.likeIconsBaseView.frame.size.height - WH) / 2, WH, WH)];
            NSURL *url = [NSURL URLWithString:obj.user.avatar];
            [imgView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"03"]];
            imgView.layer.cornerRadius = WH / 2;
            imgView.layer.masksToBounds = YES;
            [self.likeIconsBaseView addSubview:imgView];
        }];
    }
}

#pragma 设置图片
- (void) setImages {
    self.imageViewHeight.constant = self.messageModel.imagesHeight;
    if ([self.messageModel.picTotal isEqualToString:@"0"]) {
        return;
    }
    int cols = 3;

    CGFloat WH = ((ScreenWidth - ClubMargin) - (cols - 1) * (CommonMargin * 0.5)) / cols;

    [self.messageModel.pics enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGRect frame = CGRectMake((idx % cols * floor(WH)) + (idx % cols * (CommonMargin * 0.5)), (idx / cols * floor(WH)) + (idx / cols * (CommonMargin * 0.5)) , floor(WH), floor(WH));
        UIButton * button = [[UIButton alloc]init];
        button.frame = frame;
        button.imageView.layer.masksToBounds = YES;
        button.imageView.contentMode = UIViewContentModeScaleAspectFill;
        button.tag = idx;

        NSURL *url = [NSURL URLWithString:[self.messageModel.pics[idx] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

        if (url) {
            [button sd_setImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"defaultImage"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            }];
        }else {
            [button setImage:[UIImage imageNamed:@"defaultImage"] forState:UIControlStateNormal];
        }
        [button addTarget:self action:@selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.masksToBounds = YES;
        button.layer.borderColor = UIColor.lightTextColor.CGColor;
        button.layer.borderWidth = 0.2;
        [_imageBgView addSubview:button];

        if (self.messageModel.pics.count == 1) {
            button.frame = CGRectMake(5, 5, floor((ScreenWidth - ClubMargin)), floor(self.messageModel.imagesHeight - CommonMargin));
            button.layer.cornerRadius = 7;
        }else {
            button.layer.cornerRadius = 0;
        }
    }];
}

    // MARK: - 喜欢点击
- (IBAction)likeButtonAction:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(clubClickedLikeAction:likeButton:)]) {
        [self.delegate clubClickedLikeAction:self likeButton:self.likeButton];
    }
}

    // MARK: - 评论点击
- (IBAction)commentButtonAction:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(clubClickedComment:)]) {
        [self.delegate clubClickedComment:self];
    }
}

    // MARK: - 图片点击
- (void)imageButtonAction:(UIButton *)button {
    if (  [self.delegate respondsToSelector: @selector(clubSelectImage:showImage:)]) {
        [self.delegate clubSelectImage:self showImage:button.imageView.image];
    }
    
}

    // MARK: - CELL 方法
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void)prepareForReuse {
    [super prepareForReuse];
    [self.imageBgView removeAllSubviews];
    [self.likeIconsBaseView removeAllSubviews];
}

    // MARK: - TABLEVIEW 代理 数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.messageModel.comments.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XOClubCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XOClubCommentCell" forIndexPath:indexPath];

    EJSClubCommentModel *comment = self.messageModel.comments[indexPath.row];
    cell.nameLabel.text = [NSString stringWithFormat:@"%@:",comment.user.name];
    cell.commentLabel.text = [NSString stringWithFormat:@"%@",comment.content];
    cell.commentLabel.font = CHINESE_PingFangRegular(13);
    cell.nameLabel.font = CHINESE_PingFangRegular(15);
    cell.nameLabel.textColor = AppNameTextColor;
    cell.commentLabel.textColor = AppTextColor;
    return cell;
}

@end
